package springboot_shiro.com.ljs.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springboot_shiro.com.ljs.entity.Student_boot;

@Controller
public class StudentController {
/*   @GetMapping("/{url}")
    public  String test(@PathVariable("url") String url){
        return url;
        }*/
       @RequestMapping("/tologin")
        public  String tologin(){
        return  "login";
        }
    @RequestMapping("/manage")
    public  String manage(){
        return  "manage";
    }
    @RequestMapping("/rap")
    public  String rap(){
        return  "rap";
    }
    @RequestMapping("/main")
    public  String main(){
        return  "main";
    }
    @RequestMapping("/admin")
    public  String admin(){
        return  "admin";
    }

    @PostMapping("/login")
    public  String login(String name , String password, Model model){
        Subject subject=null;
        try {
        subject= SecurityUtils.getSubject();

        UsernamePasswordToken token=new UsernamePasswordToken(name,password);//获得令牌

            subject.login(token);//登录这个令牌
            Student_boot student_boot= (Student_boot) subject.getPrincipal();
            System.out.println("登录成功的用户"+student_boot);
            if(student_boot!=null){
                subject.getSession().setAttribute("student",student_boot);
                return "rap";
            }else{
                return  "login";
            }

        }catch (UnknownAccountException e){
            e.printStackTrace();
            model.addAttribute("msg","账号不存在");
            return "login";//回登录页面

        }catch (IncorrectCredentialsException exception){
            exception.printStackTrace();
            model.addAttribute("msg","密码错误");
            return "login";//回登录页面
        }



    }
    @ResponseBody
    @RequestMapping("/unauthor")
    public  String unauthor(){
        return "没有权限访问";
    }


    @RequestMapping("/logout")
    public  String logout(){
        Subject subject=SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }
//    学习vue
    @RequestMapping("/vue")
   public  String vue(){
  return  "vue";
   }
}
